//
//  hehe.swift
//  AUCharge
//
//  Created by HeHeAdmin on 2019/5/23.
//  Copyright © 2019 HeHeAdmin. All rights reserved.
//

import UIKit

extension NSObject {
    /// 字典转字符串
    ///
    /// - Parameter dic: 字典 ["key":"value"]
    /// - Returns: 返回字符串
    public class func dicValueToString(_ dic:[String : Any]) -> String {
        let data = try! JSONSerialization.data(withJSONObject: dic, options: [])
        let str = String(data: data, encoding: String.Encoding.utf8)
        return str!
    }
    
    /// 字符串转字典
    ///
    /// - Parameter str: 字符串
    /// - Returns: 字典
    public class func stringValueToDic(_ str: String) -> [String : Any]?{
        let data = str.data(using: String.Encoding.utf8)
        if let dict = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String : Any] {
            return dict
        }
        return nil
    }
    
    
    /// base64编码
    ///
    /// - Parameter plainString: 原字符串
    /// - Returns: 编码后
    public class func base64Encoding(plainString:String)->String {
        let plainData = plainString.data(using: String.Encoding.utf8)
        let base64String = plainData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
        return base64String!
    }
    /// base64解码
    ///
    /// - Parameter encodedString: 待解码的字符串
    /// - Returns: 解码后
    public class func base64Decoding(encodedString:String)->String {
        let decodedData = NSData(base64Encoded: encodedString, options: NSData.Base64DecodingOptions.init(rawValue: 0))
        let decodedString = NSString(data: decodedData! as Data, encoding: String.Encoding.utf8.rawValue)! as String
        return decodedString
    }
}
